package com.example.truthordare;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewDebug.RecyclerTraceType;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class Addplayer extends Activity {
	int gender = 1;
	boolean flag = true;
	String id, name;
	boolean t1 = false;
	int is;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addplayer);
		final EditText editName = (EditText) findViewById(R.id.editText1);
		ImageButton imgSave = (ImageButton) findViewById(R.id.add_save);
		ImageButton imgdelete = (ImageButton) findViewById(R.id.add_delete);
		ImageButton imgContact = (ImageButton) findViewById(R.id.add_contact);
		final ImageButton imggender = (ImageButton) findViewById(R.id.add_gender);

		Bundle receivebd = this.getIntent().getExtras();
		if (receivebd != null) {
			id = receivebd.getString("id");
			name = receivebd.getString("name");
			t1 = true;
			is = Integer.parseInt(id);
			editName.setText(name);

		}

		imgdelete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (t1) {
					// delete
					DBAdapter db = new DBAdapter(getApplicationContext());
					db.detele(is);
					Intent i = new Intent(getApplicationContext(),
							MainActivity.class);
					startActivity(i);
					finish();
				} else {
					// clear
					editName.setText("");
				}
			}
		});

		imgSave.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int a = editName.getText().toString().length();
				Log.i("add player", ""+a);
				if (editName.getText().toString().length() < 1) {
					Toast.makeText(getApplicationContext(),
							"Name doesn't blank ", Toast.LENGTH_SHORT).show();
					editName.setFocusable(true);
				} else {
					Pattern p = Pattern.compile("[a-z]");
					Matcher m = p.matcher(editName.getText());
					if (!m.find()) {
						Toast.makeText(getApplicationContext(),
								"Only accept [a-z]", Toast.LENGTH_SHORT).show();

					} else {
						Toast.makeText(getApplicationContext(), "t1 " + t1,
								Toast.LENGTH_SHORT).show();
						if (t1 == false) {
							Log.i("add player", "ok");
							DBAdapter db = new DBAdapter(
									getApplicationContext());
							String name = editName.getText().toString();

							String contact = null;
							db.insertPlayer(name, gender, contact);
							Intent i = new Intent(getApplicationContext(),
									MainActivity.class);
							startActivity(i);
							finish();
						} else {
							DBAdapter db = new DBAdapter(
									getApplicationContext());
							String name = editName.getText().toString();
							db.updateplayrename(is, name);
							Intent i = new Intent(getApplicationContext(),
									MainActivity.class);
							startActivity(i);
							finish();
						}
					}
				}
			}

		});

		imggender.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (flag) {

					imggender.setImageResource(R.drawable.userfemale);
					gender = 1;
					flag = false;
				} else {
					imggender.setImageResource(R.drawable.usermale);
					gender = 0;
					flag = true;
				}

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.addplayer, menu);
		return true;
	}

}
